/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controler;

import Model.Dao.DAONhanVien;
import Model.Pojo.Nhanvien;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrator
 */
public class DangNhap
        extends HttpServlet
{

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF8");
        HttpSession session = request.getSession(true);

        if (session.getAttribute("NHAN_VIEN_DANG_NHAP") != null)
        {
            PhanQuyen((Nhanvien) session.getAttribute("NHAN_VIEN_DANG_NHAP"),
                      response, request);
            return;
        }

        if (request.getParameter("user_1") != null && request.getParameter("pass_1") != null)
        {
            String user = request.getParameter("user_1");
            String pass = request.getParameter("pass_1");
            Nhanvien nv = (new DAONhanVien()).DangNhap(user, pass);

            if (nv != null)
            {
                if (nv.getTinhtrang() == true)
                {
                    session.setAttribute("NHAN_VIEN_DANG_NHAP", nv);
                    PhanQuyen(nv,
                              response, request);
                    return;
                }
                else
                {
                    request.setAttribute("ERROR", "Tài khoản của bạn đã bị khóa!!!");
                }
            }
            else
            {
                request.setAttribute("ERROR", "Tài khoản đăng nhập không hợp lệ!!!");
            }
        }
        else
        {
        }
        RequestDispatcher view = request.getRequestDispatcher("dang-nhap.jsp");
        view.forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession(true);
        session.removeAttribute("NHAN_VIEN_DANG_NHAP");
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>

    private void PhanQuyen(Nhanvien nhanvien, HttpServletResponse response,
                           HttpServletRequest request)
    {
        String url = request.getServletPath();

        List<String> Customer = new ArrayList<String>();
        Customer.add("/danh-sach-khach-hang.htm");
        Customer.add("/danh-sach-khach-hang.jsp");
        Customer.add("/them-khach-hang.htm");
        Customer.add("/them-khach-hang.jsp");
        Customer.add("/tim-kiem-khach-hang.htm");
        Customer.add("/cap-nhat-thong-tin-khach-hang.htm");

        List<String> Merchandise = new ArrayList<String>();
        Merchandise.add("/danh-sach-san-pham.htm");
        Merchandise.add("/danh-sach-san-pham.jsp");
        Merchandise.add("/them-san-pham.htm");
        Merchandise.add("/them-san-pham.jsp");
        Merchandise.add("/tim-kiem-san-pham.htm");
        Merchandise.add("/cap-nhat-san-pham.htm");

        Merchandise.add("/danh-sach-danh-muc.htm");
        Merchandise.add("/danh-sach-danh-muc.jsp");
        Merchandise.add("/them-danh-muc.htm");
        Merchandise.add("/them-danh-muc.jsp");
        Merchandise.add("/tim-kiem-danh-muc.htm");
        Merchandise.add("/update-danh-muc.htm");
        Merchandise.add("/delete-danh-muc.htm");

        List<String> Accountant = new ArrayList<String>();
        Accountant.add("/danh-sach-don-hang.htm");
        Accountant.add("/danh-sach-don-hang.jsp");
        Accountant.add("/chi-tiet-don-hang.htm");
        Accountant.add("/chi-tiet-don-hang.jsp");
        Accountant.add("/tim-kiem-don-hang.htm");

        switch (nhanvien.getLoainhanvien().getId())
        {
            case 1:
                try
                {
//                    if (!Customer.contains(url))
//                    {
//                        response.sendRedirect("danh-sach-khach-hang.htm");
//                    }
                    response.sendRedirect("danh-sach-khach-hang.htm");

                }
                catch (IOException ex)
                {
                    Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;

            case 2://Merchandise
                try
                {
//                    if (!Merchandise.contains(url))
//                    {
//                        response.sendRedirect("danh-sach-san-pham.htm");
//                    }
                    response.sendRedirect("danh-sach-san-pham.htm");

                }
                catch (IOException ex)
                {
                    Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;

            case 3://Accountant
                try
                {
//                    if (!Accountant.contains(url))
//                    {
//                        response.sendRedirect("danh-sach-don-hang.htm");
//                    }
                    response.sendRedirect("danh-sach-don-hang.htm");

                }
                catch (IOException ex)
                {
                    Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;

            case 4://System Admin
                try
                {
                    response.sendRedirect("danh-sach-nhan-vien.htm");
                }
                catch (IOException ex)
                {
                    Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
        }
    }
}
